/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package caa.struts.staffschedule.action;

import caa.struts.staffschedule.form.StaffScheduleForm;
import caa.struts.staffschedule.form.StaffSchuleListItemform;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import th.com.entity.ScheduleTable;
import th.com.entity.SemesterEntity;
import th.com.entity.UserEntity;
import th.com.ku.CAAService;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author PEDONLINE
 */
public class StaffScheduleAction extends DispatchAction {

     public ActionForward doPreviewSchedule(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
        StaffScheduleForm contentForm = (StaffScheduleForm) form;
        //contentForm.setTeachName("E9018");
        String userId = ((UserEntity)request.getSession().getAttribute("isUsername")).getUseId();
//        String ls_userId = request.getParameter("teachId");//"E9018";
//        int li_year = Integer.parseInt(request.getParameter("semId"));//3;
        Calendar now = Calendar.getInstance();
        Format formatter;
        formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        int li_NoWeekOfYear =  now.get(Calendar.WEEK_OF_YEAR);
        int li_year =  now.get(Calendar.YEAR);
        String ls_date = formatter.format(date);
        System.out.println("ls_date :"+ls_date+"li_year="+li_year);
//        int li_NoWeekOfYear = Integer.parseInt(request.getParameter("noWeekOfYear"));//48;
        ArrayList<ScheduleTable> sunTableList = new ArrayList<ScheduleTable>(0);
        ArrayList<ScheduleTable> monTableList = new ArrayList<ScheduleTable>(0);
        ArrayList<ScheduleTable> tueTableList = new ArrayList<ScheduleTable>(0);
        ArrayList<ScheduleTable> wedTableList = new ArrayList<ScheduleTable>(0);
        ArrayList<ScheduleTable> thuTableList = new ArrayList<ScheduleTable>(0);
        ArrayList<ScheduleTable> friTableList = new ArrayList<ScheduleTable>(0);
        ArrayList<ScheduleTable> satTableList = new ArrayList<ScheduleTable>(0);
        ArrayList<ScheduleTable> tb = new ArrayList<ScheduleTable>(0);
        UserEntity lo_user = CAAService.getUserByID(userId); 
//        SemesterEntity lo_sem =CAAService.getSemesterByID(li_semId);
        SemesterEntity lo_sem = CAAService.getSemesterByDate(ls_date);
        contentForm.setTeachName(lo_user.getUseTitle()+lo_user.getUseFirstName()+" "+lo_user.getUseLastName());
        contentForm.setPosition(lo_user.getUsePosition());
        contentForm.setDepName(lo_user.getDepName());
        contentForm.setFacName(lo_user.getFacName());
        contentForm.setSemSemester(lo_sem.getSemSemester());
        contentForm.setSemAcademicYear(lo_sem.getSemAcademicYear());
        tb = CAAService.selectScheduleTable(userId, li_NoWeekOfYear,li_year);
        for(int i=0;i<tb.size();i++){ 
            ScheduleTable lo_scheule = new ScheduleTable();
            lo_scheule = tb.get(i);
            if(lo_scheule != null){
                switch (lo_scheule.getNoDayOfWeek()){
                    case 0 : sunTableList.add(lo_scheule);
                        break;
                    case 1 : monTableList.add(lo_scheule);
                         break;
                    case 2 : tueTableList.add(lo_scheule);
                         break;
                    case 3 : wedTableList.add(lo_scheule);
                         break;
                    case 4 : thuTableList.add(lo_scheule);
                         break;
                    case 5 : friTableList.add(lo_scheule);
                         break;
                    case 6 : satTableList.add(lo_scheule);
                         break;
                }
                System.out.println(lo_scheule.toString());              
            }                  
               
         }       
//       contentForm.setListItem(lo_listItem);
//        System.out.println(satTableList.size());
//        contentForm.setScheduleTableList(tb);
        contentForm.setSunTableList(sunTableList);
        contentForm.setMonTableList(monTableList);
        contentForm.setTueTableList(tueTableList);
        contentForm.setWedTableList(wedTableList);
        contentForm.setThuTableList(thuTableList);
        contentForm.setFriTableList(friTableList);
        contentForm.setSatTableList(satTableList);
        
        
//        List<ScheduleEntity> rs = CAAService.selectSchedule(addConfirmClass.getKwKeyWord());
//        addConfirmClass.setScheduleList(new ArrayList<ScheduleEntity>(rs));      

        return mapping.findForward("preview.success");

    } 
    public ActionForward doShowSchedule(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        StaffScheduleForm addConfirmClass = (StaffScheduleForm) form;
        //System.out.print("ConfirmClassId " + addConfirmClass.getConfirmclassEntity().getconfId);
        System.out.print("doAddConfirmClass");

        return mapping.findForward("addconfirmclass.success");

    }
}
